﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LocalCharges.aspx.cs"
    Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <!--#include virtual="include/Head.ascx"-->
    <script type='text/javascript' src="assets/js/Carrs.js"></script>
    <script type='text/javascript' src="assets/js/Ports.js"></script>
    <script type="text/javascript">
        var sumBuyPriceUSD = 0;
        var sumSellNoVatUSD = 0;
        var sumSellWithVatUSD = 0;
        var sumBuyPriceVND = 0;
        var sumSellNoVatVND = 0;
        var sumSellWithVatVND = 0;
        function getUrlVars() {
            var vars = {};
            var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                vars[key] = value;
            });
            return vars;
        }
        $().ready(function () {
            $('#txtRates').bind('keypress', function (e) {
                if (e.keyCode == 13) {
                    var ratVal = parseFloat($(this).val().replace(/,/g, ""));
                    var totalBuy = ratVal * sumBuyPriceUSD + sumBuyPriceVND;
                    var totalSellNoVAT = ratVal * sumSellNoVatUSD + sumSellNoVatVND;
                    var totalSellWithVAT = ratVal * sumSellWithVatUSD + sumSellWithVatVND;
                    $("#totalBuy").text(formatNumber(totalBuy));
                    $("#totalSellNoVAT").text(formatNumber(totalSellNoVAT));
                    $("#totalSellWithVAT").text(formatNumber(totalSellWithVAT));

                    //Sum Total in USD
                    var totalBuy_USD = sumBuyPriceUSD + sumBuyPriceVND / ratVal;
                    var totalSellNoVAT_USD = sumSellNoVatUSD + sumSellNoVatVND / ratVal;
                    var totalSellWithVAT_USD = sumSellWithVatUSD + sumSellWithVatVND / ratVal;
                    $("#totalBuy_USD").text(formatNumber(totalBuy_USD));
                    $("#totalSellNoVAT_USD").text(formatNumber(totalSellNoVAT_USD));
                    $("#totalSellWithVAT_USD").text(formatNumber(totalSellWithVAT_USD));

                    $(this).val(formatNumber(ratVal));
                    e.preventDefault();
                }
            });

            $(".BuyPrice").live("blur", function () {
                var obj = $(this).parent().parent();
                var lcId = $(obj).attr("lcID");
                var fbuyprice = $(this).val();
                var fsellpricewithvat = fbuyprice * 1.05;
                var fsellpricenovat = fsellpricewithvat / 1.1;
                var objNoVaT = $(obj).children().find('.SellPriceNoVat');
                var objWithVaT = $(obj).children().find('.SellPriceWithVat');
                $(objNoVaT).val(fsellpricenovat.toFixed(2));
                $(objWithVaT).val(fsellpricewithvat.toFixed(2));
            });
            //VAT 10 %
            $(".SellPriceWithVat").live("blur", function () {
                var obj = $(this).parent().parent();
                var lcId = $(obj).attr("lcID");
                var fsellpricewithvat = $(this).val();
                var fsellpricenovat = fsellpricewithvat / 1.1;
                var fbuyprice = fsellpricewithvat / 1.05;
                var objBuyPrice = $(obj).children().find('.BuyPrice');
                var objNoVaT = $(obj).children().find('.SellPriceNoVat');
                $(objNoVaT).val(fsellpricenovat.toFixed(2));
                $(objBuyPrice).val(fbuyprice.toFixed(2));
            });
            $(":checkbox").live("change", function () {
                calcRate(this);
            });
            $("#checkAll").click(function () {
                var rschecked = this.checked;
                $('.cbCheckCalc').each(function (index, item) {
                    item.checked = rschecked;
                    calcRate(this);
                });
            });
            $("input[type='text']").click(function () {
                $(this).select();
            });
        });
        function calcRate(othis) {
            var obj = $(othis).parent().parent();
            var fbuyprice = $(obj).children().find('.BuyPrice').val();
            var fsellnovat = $(obj).children().find('.SellPriceNoVat').val();
            var fsellwithvat = $(obj).children().find('.SellPriceWithVat').val();
            var currencyType = $(obj).children().find('.ddCurrencyType').val();
            //alert("fbuyprice="+fbuyprice+", fsellnovat="+fsellnovat+", fsellwithvat="+fsellwithvat+", type="+currencyType);
            if (othis.checked) {
                //Cong tong                    
                if (currencyType == 2) {
                    if (fbuyprice)
                        sumBuyPriceVND += parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatVND += parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatVND += parseFloat(fsellwithvat.replace(/,/g, ""));
                }
                else {
                    if (fbuyprice)
                        sumBuyPriceUSD += parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatUSD += parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatUSD += parseFloat(fsellwithvat.replace(/,/g, ""));
                }
            }
            else {
                if (currencyType == 2) {
                    if (fbuyprice)
                        sumBuyPriceVND -= parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatVND -= parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatVND -= parseFloat(fsellwithvat.replace(/,/g, ""));
                }
                else {
                    if (fbuyprice)
                        sumBuyPriceUSD -= parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatUSD -= parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatUSD -= parseFloat(fsellwithvat.replace(/,/g, ""));
                }
            }
            //Display Sum
            $("#buyPrice_USD").text(formatNumber(sumBuyPriceUSD));
            $("#sellNoVAT_USD").text(formatNumber(sumSellNoVatUSD));
            $("#sellWithVAT_USD").text(formatNumber(sumSellWithVatUSD));
            $("#buyPrice_VND").text(formatNumber(sumBuyPriceVND));
            $("#sellNoVAT_VND").text(formatNumber(sumSellNoVatVND));
            $("#sellWithVAT_VND").text(formatNumber(sumSellWithVatVND));

            //Calc TOTAL
            var ratVal = parseFloat($("#txtRates").val().replace(/,/g, ""));
            var totalBuy = ratVal * sumBuyPriceUSD + sumBuyPriceVND;
            var totalSellNoVAT = ratVal * sumSellNoVatUSD + sumSellNoVatVND;
            var totalSellWithVAT = ratVal * sumSellWithVatUSD + sumSellWithVatVND;
            $("#totalBuy").text(formatNumber(totalBuy));
            $("#totalSellNoVAT").text(formatNumber(totalSellNoVAT));
            $("#totalSellWithVAT").text(formatNumber(totalSellWithVAT));
            //Sum Total in USD
            var totalBuy_USD = sumBuyPriceUSD + sumBuyPriceVND / ratVal;
            var totalSellNoVAT_USD = sumSellNoVatUSD + sumSellNoVatVND / ratVal;
            var totalSellWithVAT_USD = sumSellWithVatUSD + sumSellWithVatVND / ratVal;
            $("#totalBuy_USD").text(formatNumber(totalBuy_USD));
            $("#totalSellNoVAT_USD").text(formatNumber(totalSellNoVAT_USD));
            $("#totalSellWithVAT_USD").text(formatNumber(totalSellWithVAT_USD));
        }
        function OnPortSelected(source, eventArgs) {

            var hdnValueID = "<%= hidPortID.ClientID %>";
            document.getElementById(hdnValueID).value = eventArgs.get_value();
            $("#txtPortID").focus();
        }
        function OnCarrsSelected(source, eventArgs) {

            var hdnValueID = "<%= hidCarrID.ClientID %>";
            document.getElementById(hdnValueID).value = eventArgs.get_value();
            $("#txtCarr").focus();
        }
        function OnCountrySelected(source, eventArgs) {

            var hdnValueID = "<%= hidCountrySearch.ClientID %>";
            document.getElementById(hdnValueID).value = eventArgs.get_value();
        }
    </script>
    <style type="text/css">
        .thheader {
            font-weight: bold;
            vertical-align: middle;
            text-align: center;
            font-size: 11px;
            border: 1px solid #ccc !important;
            line-height: 7px !important;
            ;
        }

        .adminedit tbody tr td {
            border: 1px solid;
        }

        .bottom td {
            font-weight: bold;
            text-align: right;
        }

        #divSearch input[type=text] {
            border-radius: 7px;
            border: none;
            line-height: 22px;
            margin: 2px;
            font-size: 12px;
            padding: 3px;
        }

        .BuyPrice {
            text-align: right;
        }

        .SellPriceWithVat {
            text-align: right;
        }

        .SellPriceNoVat {
            text-align: right;
        }
    </style>
</head>
<body>
    <div id="body-wrapper">
        <div id="header">
            <!--#include virtual="include/top.ascx"-->
        </div>
        <div id="main-content1">
            <h2>LOCAL CHARGES</h2>
            <div class="content-box" style="width: 80%">
                <form id="frmLocalCosts" runat="server">

                    <div id="divSearch">
                        <div style="height: auto; padding-top: 7px;">
                            <asp:Panel ID="Panel1" runat="server" DefaultButton="btnSearch" Style="float: left;">
                                <span style="background: #e5e5e5 url('/assets/images/bg-content-box.gif') top left repeat-x; padding: 12px; border-radius: 7px; margin-right: 20px;">
                                    <asp:ToolkitScriptManager ID="ScriptManager1" runat="server">
                                    </asp:ToolkitScriptManager>
                                    <%--Carr--%>
                                    <asp:TextBox ID="txtLocalChargeID" runat="server" Visible="false"></asp:TextBox>
                                    <asp:TextBox ID="txtCarr" name="txtCarr" runat="server" Width="50px" PlaceHolder="Carr" BackColor="AliceBlue" Font-Bold="true"></asp:TextBox>
                                    <asp:HiddenField ID="hidCarrID" runat="server" />
                                    <asp:AutoCompleteExtender ID="AutoCompleteExtender5" runat="server" ServiceMethod="CarrsAutoCompleteAjaxRequest"
                                        ServicePath="Webservices/AutoComplete.asmx" MinimumPrefixLength="2" CompletionInterval="100"
                                        EnableCaching="true" CompletionSetCount="10" TargetControlID="txtCarr" FirstRowSelected="true"
                                        CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"
                                        CompletionListHighlightedItemCssClass="itemHighlighted" DelimiterCharacters=","
                                        OnClientItemSelected="OnCarrsSelected">
                                    </asp:AutoCompleteExtender>

                                    <%--PORT--%>
                                    <asp:TextBox ID="txtPort" name="txtPort" runat="server" Width="120px" PlaceHolder="Port"
                                        BackColor="AliceBlue" Font-Bold="true"></asp:TextBox>
                                    <asp:HiddenField ID="hidPortID" runat="server" />
                                    <asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" ServiceMethod="PortChargeAutoCompleteAjaxRequest"
                                        ServicePath="Webservices/AutoComplete.asmx" MinimumPrefixLength="2" CompletionInterval="100"
                                        EnableCaching="true" CompletionSetCount="10" TargetControlID="txtPort" FirstRowSelected="true"
                                        CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"
                                        CompletionListHighlightedItemCssClass="itemHighlighted" DelimiterCharacters=","
                                        OnClientItemSelected="OnPortSelected">
                                    </asp:AutoCompleteExtender>
                                    <%--Country--%>
                                    <asp:TextBox class="cls" ID="txtCountrySearch" runat="server" Width="90px" PlaceHolder="To Country"></asp:TextBox>
                                    <asp:HiddenField ID="hidCountrySearch" runat="server"></asp:HiddenField>
                                    <asp:AutoCompleteExtender ID="AutoCompleteExtenderCountry" runat="server" ServiceMethod="CountryAutoCompleteAjaxRequest"
                                        ServicePath="Webservices/AutoComplete.asmx" MinimumPrefixLength="2" CompletionInterval="100"
                                        EnableCaching="true" CompletionSetCount="10" TargetControlID="txtCountrySearch"
                                        FirstRowSelected="true" CompletionListCssClass="completionList" CompletionListItemCssClass="listItem"
                                        CompletionListHighlightedItemCssClass="itemHighlighted" DelimiterCharacters=","
                                        OnClientItemSelected="OnCountrySelected">
                                    </asp:AutoCompleteExtender>
                                    <asp:DropDownList ID="ddExpImp" runat="server" OnSelectedIndexChanged="btnSearch_Click">
                                        <%--<asp:ListItem Text="Select" Value=""></asp:ListItem>--%>
                                        <asp:ListItem Text="EXP" Value="1"></asp:ListItem>
                                        <asp:ListItem Text="IMP" Value="2"></asp:ListItem>
                                    </asp:DropDownList>
                                    <asp:Button class="button" ID="btnSearch" runat="server" Text="Search"
                                        OnClick="btnSearch_Click" />
                                </span>
                            </asp:Panel>
                            <div>
                                <input type="text" id="txtRates" runat="server" placeholder="Input Rate" visible="false" style="float: right; margin-right: 5px; border: 1px solid; width: 100px;" />
                                <asp:TextBox ID="txtLocalChargeNote" runat="server" Width="300px" Visible="false" PlaceHolder="Note" Style="float: right; border: 1px solid;"></asp:TextBox>
                            </div>
                        </div>
                        <div class="clear">
                        </div>
                        <div style="margin-top: 10px; margin-left: 5px;">
                            <asp:Label ID="lblError" CssClass="msg-error" runat="server"></asp:Label><br />
                        </div>
                    </div>

                    <div class="content-box-header">
                        <h3 style="cursor: s-resize;"></h3>
                        <asp:Button class="button" ID="btnSave" runat="server" Text="Save"
                            OnClick="btnSave_Click" Style="float: right; margin: 5px;" />
                        <div class="clear">
                        </div>
                    </div>
                    <fieldset>
                        <asp:Repeater ID="rptData" runat="server" OnItemDataBound="rptData_ItemDataBound">
                            <ItemTemplate>
                                <tr onmouseover="this.style.background='#dbf0c9'" onmouseout="this.style.background='#ffffff'"
                                    lcindex='<%#Eval("Index") %>' lcid='<%#Eval("ID") %>'>
                                    <td align="center">
                                        <asp:HiddenField ID="hidID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ID")%>'></asp:HiddenField>
                                        <input type="CheckBox" value='<%#Eval("Index") %>' id="cbSelect" name="cbSelect"
                                            class="cbCheckCalc" />
                                    </td>
                                    <td align="center">
                                        <asp:HiddenField ID="ChargeCode" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ChargeCode")%>'></asp:HiddenField>
                                        <asp:DropDownList ID="ddChargeType" runat="server" CssClass="ddChargeType">
                                        </asp:DropDownList>
                                    </td>
                                    <td align="center">
                                        <asp:HiddenField ID="UnitCode" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "UnitCode")%>'></asp:HiddenField>
                                        <asp:DropDownList ID="ddUnitTransport" runat="server" CssClass="ddUnitTransport">
                                        </asp:DropDownList>
                                    </td>
                                    <td align="center">
                                        <asp:TextBox ID="CurrCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CurrCode")%>'
                                            Style="display: none;" CssClass="TypeOfCurrency"></asp:TextBox>
                                        <asp:DropDownList ID="ddCurrencyType" runat="server" CssClass="ddCurrencyType">
                                            <asp:ListItem Text="USD" Value="1"></asp:ListItem>
                                            <asp:ListItem Text="VND" Value="2"></asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="BuyPrice" runat="server" class="BuyPrice"
                                            Text='<%# DataBinder.Eval(Container.DataItem, "BuyPrice")%>'></asp:TextBox>
                                    </td>

                                    <td>
                                        <asp:TextBox ID="SellPriceWithVat" runat="server" class="SellPriceWithVat"
                                            Text='<%# DataBinder.Eval(Container.DataItem, "SellPriceWithVat")%>'></asp:TextBox>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="SellPriceNoVat" runat="server" class="SellPriceNoVat"
                                            Text='<%# DataBinder.Eval(Container.DataItem, "SellPriceNoVat")%>'></asp:TextBox>
                                    </td>
                                    <td>
                                        <%#Eval("Action") %>
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <HeaderTemplate>
                                <table cellspacing="0" cellpadding="0" width="100%" border="0" class="adminlist"
                                    id="tableCharge">
                                    <thead>
                                        <tr>
                                            <th align="center" rowspan="2">
                                                <input type="CheckBox" id="checkAll" name="checkAll" />
                                            </th>
                                            <th align="center" rowspan="2">Local
                                            </th>
                                            <th align="center" rowspan="2">Unit
                                            </th>
                                            <th align="center" rowspan="2">Curr
                                            </th>
                                            <th align="center" colspan="3">Charges
                                            </th>
                                            <th rowspan="2">#</th>
                                        </tr>
                                        <tr>
                                            <th align="center">Buy
                                            </th>
                                            <th align="center">Sell + VAT
                                            </th>
                                            <th align="center">Sell - VAT
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>
                            </HeaderTemplate>
                            <FooterTemplate>
                                </tbody>
                            <tr class="bottom" style="display: none;">
                                <td colspan="3" rowspan="2">Sub Total
                                </td>
                                <td>USD
                                </td>
                                <td id="buyPrice_USD"></td>
                                <td id="sellNoVAT_USD"></td>
                                <td id="sellWithVAT_USD"></td>
                                <td></td>
                            </tr>
                                <tr class="bottom" style="display: none;">
                                    <td>VND
                                    </td>
                                    <td id="buyPrice_VND"></td>
                                    <td id="sellNoVAT_VND"></td>
                                    <td id="sellWithVAT_VND"></td>
                                    <td></td>
                                </tr>
                                <tr class="bottom">
                                    <td colspan="4" style="text-align: left; font-size: 12px;">Sum Total in USD
                                    </td>
                                    <td id="totalBuy_USD"></td>
                                    <td id="totalSellWithVAT_USD"></td>
                                    <td id="totalSellNoVAT_USD"></td>
                                    <td></td>
                                </tr>
                                <tr class="bottom">
                                    <td colspan="4" style="text-align: left; font-size: 12px;">Sum Total in VND
                                    </td>
                                    <td id="totalBuy"></td>
                                    <td id="totalSellWithVAT"></td>
                                    <td id="totalSellNoVAT"></td>
                                    <td></td>
                                </tr>
                                </TABLE>
                            </FooterTemplate>
                        </asp:Repeater>
                    </fieldset>
                    <div class="clear">
                    </div>
                    <div id="footer">
                    </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
